<script>
// app.js
// import config from './config'
// import Mock from './mock/index'
import createBus from "./utils/eventBus";
import { connectSocket, fetchUnreadNum } from "./services/chat";
import { getUserInfo, logout } from "./api/user";
import config from "./config";
const { baseUrl } = config;
const imageBaseUrl = `${baseUrl}/file?uri=`;
// const svg_xzcf = require("./static/icons/行政处罚.svg");

// if (config.isMock) {
//   Mock()
// }

export default {
  data() {
    return {};
  },
  onLaunch() {
    if (uni.getUpdateManager) {
      const updateManager = uni.getUpdateManager();
      updateManager.onCheckForUpdate((res) => {
        // console.log(res.hasUpdate)
      });
      updateManager.onUpdateReady(() => {
        uni.showModal({
          title: "更新提示",
          content: "新版本已经准备好，是否重启应用？",
          success(res) {
            if (res.confirm) {
              updateManager.applyUpdate();
            }
          },
        });
      });
    }

    this.globalData.getUnreadNum();
    this.globalData.connect();
    this.globalData.getUserInfoFn();
  },
  globalData: {
    userInfo: null,
    unreadNum: 0,

    // 未读消息数量
    socket: null,

    // SocketTask 对象
    imageBaseUrl,

    iconMap: {
      ups: "/static/images/ups.png",
      jk: "/static/images/jk.png",
      deviceUps: "/static/images/device_ups.png",
      deviceJk: "/static/images/device_jk.png",
      point: "/static/images/point.png",
      icon: "/static/images/location.png",
    },

    // 图标映射
    limit: 50,

    // 列表最多50条
    deviceStatusColorMap: {
      // 1：正常，2：离线，3：异常
      1: "#00B050",
      2: "#FFC000",
      3: "#FF0000",
    },

    navList: [
      {
        title: "任务列表",
        cercleColor: "#FF9648",
        iconColor: "#ffffff",
        icon: "list",
        iconWidth: 52,
        iconHeight: 70,
        url: "",
        // msgJumpUrl: '/pages/fireDtailNew/index',
        num: 0,
        authority: "/wxapp/company/index",
      },
      {
        title: "隐患上报",
        cercleColor: "#FF6161",
        iconColor: "#ffffff",
        icon: "error-triangle",
        iconWidth: 62,
        iconHeight: 51,
        url: "/pages/hiddenDangerReport/index",
        msgJumpUrl: "",
        num: 0,
        authority: "",
      },
      {
        title: "隐患整改",
        iconColor: "#ffffff",
        cercleColor: "#8D8DFF",
        icon: "secured",
        iconWidth: 49,
        iconHeight: 59,
        url: "/pages/hiddenDangerList/index",
        num: 0,
        authority: "",
      },
      {
        title: "行政处罚",
        iconColor: "#ffffff",
        cercleColor: "#51C5FF",
        iconSrc: "/static/icons/行政处罚.svg",
        iconWidth: 40,
        iconHeight: 40,
        url: "/pages/administrativePenalty/index",
        num: 0,
        authority: "",
      },
      {
        title: "企业信息",
        iconColor: "#ffffff",
        cercleColor: "#4AEDCD",
        icon: "info-circle",
        iconWidth: 56,
        iconHeight: 56,
        url: "/pages/companyInfo/index",
        num: 0,
        authority: "",
      },
      // {
      //   title: '任务列表',
      //   cercleColor: '#D7E1F5',
      //   icon: '/images/gongsi.png',
      //   iconWidth: 42,
      //   iconHeight: 51,
      //   url: '/pages/warnInfo/index',
      //   num: 0,
      // },
    ],

    /** 全局事件总线 */
    eventBus: createBus(),

    /** 初始化WebSocket */
    connect() {
      const socket = connectSocket();
      socket.onMessage((data) => {
        data = JSON.parse(data);
        if (data.type === "message" && !data.data.message.read) {
          this.setUnreadNum(this.unreadNum + 1);
        }
      });
      this.socket = socket;
    },

    /** 获取未读消息数量 */
    getUnreadNum() {
      fetchUnreadNum().then(({ data }) => {
        this.unreadNum = data;
        this.eventBus.emit("unread-num-change", data);
      });
    },

    /** 设置未读消息数量 */
    setUnreadNum(unreadNum) {
      this.unreadNum = unreadNum;
      this.eventBus.emit("unread-num-change", unreadNum);
    },

    /**
     * 获取用户信息
     */
    async getUserInfoFn() {
      try {
        let res = uni.getStorageSync("userInfo");
        if (!res) {
          res = await getUserInfo();
        }
        this.userInfo = res;
        return res;
      } catch (error) {
        console.log("CatchClause", error);
        console.log("CatchClause", error);
        console.log("获取用户信息失败：", error);
        return null;
      }
    },

    /** 退出登录 */
    logoutFn() {
      try {
        logout();
        uni.clearStorageSync();
        uni.reLaunch({
          url: "/pages/login/login",
        });
      } catch (error) {
        console.log("CatchClause", error);
        console.log("CatchClause", error);
        console.log("退出登录失败：", error);
      }
    },
  },
};
</script>
<style lang="less">
/**app.wxss**/
/*每个页面公共css */
// @import "@/uni_modules/tdesign-ui/index.less";

view,
input {
  box-sizing: border-box;
}

.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
}
</style>
